package com.example.androiduserform;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class AndroidUserForm extends Activity
{
	AutoCompleteTextView actvFavColor;
	Button btnSubmit;
	DatePicker dpBirth;
	EditText etName, etSurname, etDescription, etBlog, etContact;
	MultiAutoCompleteTextView mactvLanguages;
	SeekBar sbFbAccount;
	Spinner sGender;
	TextView tvFbAccountSinceWhen;
	ToggleButton tbFbAccount;
	ContextMenu cmForm;
	Boolean contextEditSurname = false;
	TableLayout tableLayout;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_android_user_form);
		this.Initialize();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		Log.d("EVENT", "CreateContextMenu");
		super.onCreateContextMenu(menu, v, menuInfo);
		switch (v.getId())
		{
		case R.id.tlMain:
			if (contextEditSurname)
			{
				contextEditSurname = false;
				return;
			}
			menu.setHeaderTitle("MAIN");
			getMenuInflater().inflate(R.menu.form, menu);
			break;
		case R.id.etSurname:
			contextEditSurname = true;
			getMenuInflater().inflate(R.menu.edit_text, menu);
			menu.setHeaderTitle("EDIT");
			break;
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		EditText et;
		switch (item.getItemId())
		{
		case R.id.formItemClear:
			clearForm();
			return true;
		case R.id.editItemCapital:
			et = (EditText) findViewById(R.id.etSurname);
			String value = et.getText().toString();
			et.setText(value.toUpperCase());
			return true;
		case R.id.editItemClear:
			et = (EditText) findViewById(R.id.etSurname);
			et.setText("");
			return true;
		}
		return false;
	}

	private void Initialize()
	{
		etName = (EditText) findViewById(R.id.etName);
		etSurname = (EditText) findViewById(R.id.etSurname);
		dpBirth = (DatePicker) findViewById(R.id.dpBirth);
		etDescription = (EditText) findViewById(R.id.etDescription);
		etBlog = (EditText) findViewById(R.id.etBlog);
		etContact = (EditText) findViewById(R.id.etContact);
		actvFavColor = (AutoCompleteTextView) findViewById(R.id.actvFavColor);
		mactvLanguages = (MultiAutoCompleteTextView) findViewById(R.id.mactvLanguages);
		sGender = (Spinner) findViewById(R.id.sGender);
		tbFbAccount = (ToggleButton) findViewById(R.id.tbFbAccount);
		sbFbAccount = (SeekBar) findViewById(R.id.sbFbAccount);
		tvFbAccountSinceWhen = (TextView) findViewById(R.id.tvFbAccountSinceWhen);
		btnSubmit = (Button) findViewById(R.id.btnSubmit);
		tableLayout = (TableLayout) findViewById(R.id.tlMain);

		registerForContextMenu(tableLayout);
		registerForContextMenu(etSurname);

		/* Set default values */

		dpBirth.setMaxDate(Calendar.getInstance().getTimeInMillis());

		actvFavColor.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { getString(R.string.colorRed),
				getString(R.string.colorGreen), getString(R.string.colorBlue), getString(R.string.colorOther) }));

		mactvLanguages.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { getString(R.string.langPOL),
				getString(R.string.langENG), getString(R.string.langFRA), getString(R.string.langGER) }));

		sGender.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { getString(R.string.genderUnknown),
				getString(R.string.genderMale), getString(R.string.genderFemale) }));

		sbFbAccount.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
		{

			public void onStopTrackingTouch(SeekBar seekBar)
			{
				// TODO Auto-generated method stub
			}

			public void onStartTrackingTouch(SeekBar seekBar)
			{
				// TODO Auto-generated method stub
			}

			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
			{
				tvFbAccountSinceWhen.setText(getString(R.string.fbPeriodPrefix) + " " + progress + " " + getString(R.string.fbPeriodSuffix));
			}
		});

		tvFbAccountSinceWhen.setText(getString(R.string.fbPeriodPrefix) + " " + sbFbAccount.getProgress() + " " + getString(R.string.fbPeriodSuffix));

		btnSubmit.setOnClickListener(new OnClickListener()
		{
			public void onClick(View arg0)
			{
				AlertDialog alertDialog = new AlertDialog.Builder(AndroidUserForm.this).create();
				alertDialog.setTitle("Podsumowanie formularza");
				String msg = getString(R.string.name)
						+ ": "
						+ etName.getText()
						+ "\n"
						+ getString(R.string.surname)
						+ ": "
						+ etSurname.getText()
						+ "\n"
						+ getString(R.string.birthDate)
						+ ": "
						+ dpBirth.getDayOfMonth()
						+ "/"
						+ dpBirth.getMonth()
						+ "/"
						+ dpBirth.getYear()
						+ "\n"
						+ getString(R.string.description)
						+ ": "
						+ etDescription.getText()
						+ "\n"
						+ getString(R.string.blog)
						+ ": "
						+ etBlog.getText()
						+ "\n"
						+ getString(R.string.contact)
						+ ": "
						+ etContact.getText()
						+ "\n"
						+ getString(R.string.favColor)
						+ ": "
						+ actvFavColor.getText()
						+ "\n"
						+ getString(R.string.languages)
						+ ": "
						+ mactvLanguages.getText()
						+ "\n"
						+ getString(R.string.gender)
						+ ": "
						+ sGender.getSelectedItem()
						+ "\n"
						+ getString(R.string.fbAccount)
						+ ": "
						+ tbFbAccount.getText()
						+ "\n"
						+ (tbFbAccount.getText().toString() == getString(R.string.fbAccountTrue) ? (getString(R.string.fbAccountSinceWhen) + ": "
								+ getString(R.string.fbPeriodPrefix) + " " + sbFbAccount.getProgress() + " " + getString(R.string.fbPeriodSuffix)) : "");

				final SpannableString s = new SpannableString(msg);
				Linkify.addLinks(s, Linkify.ALL);
				alertDialog.setMessage(s);
				alertDialog.show();
				((TextView) alertDialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
			}
		});
	}

	private void clearForm()
	{
		etName.setText("");
		etSurname.setText("");
		etDescription.setText("");
		etBlog.setText("");
		etContact.setText("");
		actvFavColor.setText("");
		mactvLanguages.setText("");
		sGender.setSelection(0);
		tbFbAccount.setChecked(false);
		sbFbAccount.setProgress(0);
		tvFbAccountSinceWhen.setText(getString(R.string.fbPeriodPrefix) + " " + sbFbAccount.getProgress() + " " + getString(R.string.fbPeriodSuffix));
	}
}